package com.bwie.system.controller;

import com.bwie.common.domain.Project;
import com.bwie.common.domain.request.ProjectReq;
import com.bwie.common.domain.response.ProTypeResp;
import com.bwie.common.domain.response.ProjectResp;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.common.utils.StringUtils;
import com.bwie.system.oss.OssUtil;
import com.bwie.system.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.OutputStream;
import java.util.List;

/**
 * @className: ProjectController
 * @Description: TODO
 * @version: v1.８.0
 * @author:
 * @date:
 */
@RestController
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private OssUtil ossUtil;

    /**
     * 项目列表
     * @param projectReq
     * @return
     */
    @PostMapping("/projectList")
    public PageResult<ProjectResp> projectList(@RequestBody ProjectReq projectReq){
        PageResult<ProjectResp> result = projectService.projectList(projectReq);
        return result;
    }

    /**
     *类型费用审核记录表
     * @param projectReq
     * @return
     */
    @PostMapping("/proTypeList")
    public List<ProTypeResp> proTypeList(@RequestBody ProjectReq projectReq){
        List<ProTypeResp> proTypeResps = projectService.proTypeList(projectReq);
        return proTypeResps;
    }

    /**
     * 新增项目申请表
     * @param projectReq
     * @return
     */
    @PostMapping("/addProject")
    public Result addProject(@RequestBody ProjectReq projectReq){
        projectService.addProject(projectReq);
        return Result.success();
    }

    /**
     * 图片上传限制大小
     * @param file
     * @return
     */
    @PostMapping("/uploadImg")
    public Result<String> uploadImg(@RequestParam("file") MultipartFile file){

        if(file.getSize()>10*1024*1024){
            return Result.error("文件的大小不能超过10MB");
        }
        //查看文件类型
        String contentType = file.getContentType();

        String path = ossUtil.uploadMultipartFile(file);
        return Result.success(path);
    }

    /**
     * 填报项目预估价格    审核员审核通过
     * @param projectReq
     * @return
     */
    @PostMapping("/updProject")
    public Result updProject(@RequestBody ProTypeResp projectReq){
        projectService.updProject(projectReq);
        return Result.success();
    }
}
